package com.jcfk.eam.service.base;

import com.jcfk.common.business.enums.DeleteFlag;
import com.jcfk.eam.domain.dto.base.EamAssetBrandDTO;
import com.jcfk.eam.domain.dto.base.EamAssetTypeDTO;
import com.jcfk.eam.domain.dto.base.EamMakerDTO;
import com.jcfk.eam.domain.po.base.EamAssetModel;
import com.jcfk.eam.domain.po.base.EamAssetType;
import com.jcfk.eam.domain.query.base.EamAssetModelQuery;
import com.jcfk.eam.domain.dto.base.EamAssetModelDTO;
import com.jcfk.eam.dao.base.EamAssetModelDao;
import com.jcfk.common.business.service.BaseService;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.RetResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jcfk.eam.domain.uploadExcel.base.EamAssetModelUploadExcel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;
import com.jcfk.common.core.utils.BeanUtils;
import cn.hutool.core.util.StrUtil;
import com.jcfk.common.security.utils.SecurityUtils;
import com.alibaba.druid.util.StringUtils;

import java.util.List;
import java.util.ArrayList;
import static com.jcfk.common.core.utils.BeanUtils.convertList;

import java.util.Date;

/**
 * 资产型号
 *
 * @author yql
 * @email 522891237@gqq.com
 * @date 2024-07-24
 */
@Service
public class EamAssetModelService extends BaseService<EamAssetModelDao,EamAssetModel> {

    @Autowired
    private EamMakerService eamMakerService;

    @Autowired
    private EamAssetTypeService eamAssetTypeService;

    @Autowired
    private EamAssetBrandService eamAssetBrandService;


    /**
     * EamAssetModel分页查询
     *
     * @param page  分页设置
     * @param query 查询条件
     * @return 分页查询结果
     */
    public DataPage<EamAssetModelDTO> queryByPage(DataPage<EamAssetModelDTO> page, EamAssetModelQuery query) {
        // 分页查询
        query.setModelNo(StringUtils.isEmpty(query.getModelNo()) ? query.getModelNo() : query.getModelNo() + "%");
        page = this.baseMapper.queryByPage(page, query);
        return page;
    }
    public List<EamAssetModelDTO> queryByPage(EamAssetModelQuery query) {
        // 分页查询
        query.setModelNo(StringUtils.isEmpty(query.getModelNo()) ? query.getModelNo() : query.getModelNo() + "%");
        return this.baseMapper.queryByPage(query);
    }

    /**
     * 判断EamAssetModel是已经存在
     *
     * @param dto
     * @return
     */
    public RetResult<String> isValid(EamAssetModelDTO dto) {
        QueryWrapper<EamAssetModel> qw = new QueryWrapper<>();
        Integer count = 0;
        // 判断编号
        if (StrUtil.isNotEmpty(dto.getModelNo())) {
            // 修改时添加条件
            if (!StrUtil.isEmpty(dto.getId())) {
                qw.lambda().ne(EamAssetModel::getId, dto.getId());
            }
            qw.lambda().eq(EamAssetModel::getAssetKind, dto.getAssetKind());
            qw.lambda().eq(EamAssetModel::getAssetTypeId, dto.getAssetTypeId());
            qw.lambda().eq(EamAssetModel::getModelNo, dto.getModelNo());
            qw.lambda().eq(EamAssetModel::getDeleted, DeleteFlag.NOTDELETED.getValue());

            count = this.baseMapper.selectCount(qw);
            if (count > 0) {
                return RetResult.fail("型号已存在");
            }
        }
        return RetResult.ok();
    }

    /**
     * 插入资产型号
     *
     * @param dto 插入对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void insert(EamAssetModelDTO dto) {

        // 保存
        EamAssetModel item = new EamAssetModel();
        BeanUtils.copyProperties(dto, item);
        this.setDefaultValue(item);

        super.save(item);
    }

    /**
     * 更新资产型号
     *
     * @param dto 更新对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void update(EamAssetModelDTO dto) {

        // 保存
        EamAssetModel item = new EamAssetModel();
        BeanUtils.copyProperties(dto, item);

        // 默认数据
        this.setDefaultValue(item);

        super.updateById(item);
    }

    /**
     * 根据批量主键删除资产型号(逻辑删除)
     *
     * @param dto
     * @return bool
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean logicDeleteList(EamAssetModelDTO dto) {

        EamAssetModel item = new EamAssetModel();

        this.setDefaultValue(item);
        item.setVersion(dto.getVersion());
        item.setDeleted(1);

        QueryWrapper<EamAssetModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", dto.getIds());

        return this.update(item, queryWrapper);
    }

    /**
     * 设置添加默认值
     *
     * @param item 添加对象
     */
    public void setDefaultValue(EamAssetModel item) {
        if (StrUtil.isEmpty(item.getCreatedUserId())) {
            item.setCreatedUserId(SecurityUtils.getUserId());
            item.setCreateTime(new Date());
            item.setVersion(1L);
            item.setDeleted(0);
            item.setCreatedUserName(SecurityUtils.getUserName());
        }
        item.setLastUpdatedUserId(SecurityUtils.getUserId());
        item.setLastUpdateTime(new Date());

        item.setLastUpdatedUserName(SecurityUtils.getUserName());

    }

    /**
     * 根据主键获取资产型号信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public EamAssetModelDTO get(String id) {
        // 查询数据
        EamAssetModel item = this.baseMapper.selectById(id);
        EamAssetModelDTO dto = new EamAssetModelDTO();
        try {
            BeanUtils.copyProperties(item, dto);

            if(dto.getMakerId() != null && !dto.getMakerId().isEmpty()){
                //制造商名称
                EamMakerDTO maker = eamMakerService.get(dto.getMakerId());
                if(maker != null) {
                    dto.setMakerName(maker.getMakerName());
                    dto.setMakerCode(maker.getMakerCode());
                }
            }
            if(dto.getBrandId() != null && !dto.getBrandId().isEmpty()){
                //品牌名称
                EamAssetBrandDTO brand = eamAssetBrandService.get(dto.getBrandId());
                if(brand != null) {
                    dto.setBrandName(brand.getBrandName());
                    dto.setBrandCode(brand.getBrandCode());
                }
            }
            if(dto.getAssetTypeId() != null && !dto.getAssetTypeId().isEmpty()){
                //类型名称
                EamAssetTypeDTO assetType = eamAssetTypeService.get(dto.getAssetTypeId());
                if(assetType != null) {
                    dto.setAssetTypeName(assetType.getAssetTypeName());
                    dto.setAssetTypeCode(assetType.getAssetTypeCode());
                }
            }
        } catch (RuntimeException e) {
            throw e;
        }

        return dto;
    }


    /**
     * 插入资产型号
     *
     * @param list 批量插入资产型号
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void insertBatch(List<EamAssetModelUploadExcel> list) {

        List<EamAssetModel> savelist = new ArrayList<>();
        list.forEach(itemOne -> {
            // 保存
            EamAssetModel item = new EamAssetModel();
            BeanUtils.copyProperties(itemOne, item);
            this.setDefaultValue(item);
            savelist.add(item);
        });
        // 保存
        if (savelist.size() > 0) super.saveBigBatch(savelist);
    }

    /**
     * 根据条件获取单表信息列表
     *
     * @param query 查询条件
     * @return List<EamAssetModelDTO>
     */
    public List<EamAssetModelDTO> getList(EamAssetModelQuery query) {
        // 查询条件
        QueryWrapper<EamAssetModel> qw = new QueryWrapper<>();
        qw.lambda().eq(!StrUtil.isEmpty(query.getAssetTypeId()), EamAssetModel::getAssetTypeId, query.getAssetTypeId());
        qw.lambda().eq(!StrUtil.isEmpty(query.getMakerId()), EamAssetModel::getMakerId, query.getMakerId());
        qw.lambda().eq(!StrUtil.isEmpty(query.getBrandId()), EamAssetModel::getBrandId, query.getBrandId());
        qw.lambda().eq(!StrUtil.isEmpty(query.getModelNo()), EamAssetModel::getModelNo, query.getModelNo());
        //qw.lambda().likeRight(!StrUtil.isEmpty(query.getModelNo()), EamAssetModel::getModelNo, query.getModelNo());
        qw.lambda().eq(query.getAssetKind() != null, EamAssetModel::getAssetKind, query.getAssetKind());
        qw.lambda().eq(query.getEnabled() != null, EamAssetModel::getEnabled, query.getEnabled());
        qw.lambda().eq(query.getDeleted() != null, EamAssetModel::getDeleted, query.getDeleted());
        // 查询数据
        List<EamAssetModel> list = super.list(qw);

        return convertList(list, EamAssetModelDTO.class);
    }

    /**
     * 根据主键获取资产型号信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public void deleteitem(String id) {
        EamAssetModel item = getById(id);
        this.setDefaultValue(item);
        item.setDeleted(1);

        this.updateById(item);
    }
}

